﻿
using System;
using System.Collections.Generic;
using System.IO;


namespace Lon.Server
{

    internal class ConnectStatusProcess : DataProcessProvider
    {

        private NetNodeManager csmNodeManager;
        private FrontServerNode frontNode;

        public ConnectStatusProcess(NetNodeManager nodeManager, FrontServerNode frontServer)
        {
            this.csmNodeManager = nodeManager;
            this.frontNode = frontServer;

        }



        private byte[] PackNodeStatusPackage(List<NetNode> dbmList)
        {

            byte[] buf = new byte[4 + (dbmList.Count * 4)];
            BinaryWriter bw = new BinaryWriter(new MemoryStream(buf));
            bw.Write((int)(dbmList.Count * 4));
            foreach (NetNode node in dbmList)
            {
                int id = node.Id;
                bw.Write(id);
                bw.BaseStream.Seek(-1, SeekOrigin.Current);
                byte status = node.ConnectedState ? ((byte)1) : ((byte)0);
                bw.Write(status);
            }
            return buf;

        }

        protected override void ProcessCommandPackage(DataFrame rPkg)
        {
            if (rPkg.DataBuf != null)
            {
                if (rPkg.DataBuf.Length < 4)
                {
                    return;
                }

                try
                {
                    List<NetNode> nodeList = this.csmNodeManager.GetNodeList();
                    byte[] status = this.PackNodeStatusPackage(nodeList);
                    DataFrame df = new DataFrame(rPkg.SrcId, rPkg.DstId, DataDir.StationData, false, false, false, 0x23, 0, status);

                }
                catch (Exception e)
                {
                    
                }
            }
           
        }

        protected override void ProcessDataPackage(DataFrame rPkg)
        {

            int reporterId = rPkg.SrcId;
            NetNode fnode = this.csmNodeManager.GetNode(reporterId);
            if (fnode == null) return;
            if (fnode.NodeType != NodeType.FrontServer) return;

            Dictionary<string, NetNode> curNodeList = new Dictionary<string, NetNode>();
            try
            {
                BinaryReader br = new BinaryReader(new MemoryStream(rPkg.DataBuf));
                int count = br.ReadInt32() / 4;
                for (int i = 0; i < count; i++)
                {
                    int id = br.ReadInt32();
                    id &= 0xffffff;
                    br.BaseStream.Seek(-1, SeekOrigin.Current);
                    bool state = br.ReadByte() == 1;
                    NetNode node = this.csmNodeManager.GetNode(id);
                    if (node == null) continue;
                    node.ConnectedState = state;
                    node.ParentServer = this.frontNode;
                    
                    
                 

                }
                br.Close();
            }
            catch (Exception e)
            {
              
                return;
            }
      
        }

    }
}

